package com.jsmile.cloud.tradecenter.cart.feign;

import javax.validation.Valid;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.jsmile.cloud.tradecenter.api.req.cart.ReqAddCart;
import com.jsmile.cloud.tradecenter.api.req.cart.ReqCartDetial;
import com.jsmile.cloud.tradecenter.api.req.cart.ReqDeleteCart;
import com.jsmile.cloud.tradecenter.api.req.cart.ReqUpdateCart;
import com.jsmile.cloud.tradecenter.cart.factory.CartFactory;
import com.jsmile.mail.security.controller.AuthcController;
import com.jsmile.mail.security.handler.JsmileContext;
import com.jsmile.mall.api.JSmileResult;
import com.jsmile.mall.api.annotation.CurrentContext;
import com.jsmile.mall.api.constants.JSmileConstant;
import com.jsmile.mall.api.enums.LogTypeEnum;
import com.jsmile.mall.log.annotation.JsmileLog;

import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
@AllArgsConstructor
@Api(tags = "购物车管理")
public class CartServiceApiImpl extends AuthcController {

    private final CartFactory cartFactory;

    @RequestMapping(value = "/feign/cart/addCart", method = RequestMethod.POST)
    @JsmileLog(title = "订单中心->添加购物车", type = LogTypeEnum.INSERT, systemId = JSmileConstant.SystemId.TRADE_CENTER)
    public JSmileResult addCart(@RequestBody @Valid ReqAddCart reqAddCart, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.TRADE_CENTER);
        reqAddCart.setCurUserId(jsmileContext.getUserId());
        reqAddCart.setCurTenantId(jsmileContext.getTenantId());
        return JSmileResult.success(cartFactory.addCart(reqAddCart));
    }

    @RequestMapping(value = "/feign/cart/cartDetial", method = RequestMethod.POST)
    @JsmileLog(title = "订单中心->购物车详情", type = LogTypeEnum.QUERY, systemId = JSmileConstant.SystemId.TRADE_CENTER)
    public JSmileResult cartDetial(@RequestBody @Valid ReqCartDetial reqCartDetial, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.TRADE_CENTER);
        reqCartDetial.setCurUserId(jsmileContext.getUserId());
        reqCartDetial.setCurTenantId(jsmileContext.getTenantId());
        return JSmileResult.success(cartFactory.cartDetial(reqCartDetial));
    }

    @RequestMapping(value = "/feign/cart/updateCart", method = RequestMethod.POST)
    @JsmileLog(title = "订单中心->更新购物车", type = LogTypeEnum.UPDATE, systemId = JSmileConstant.SystemId.TRADE_CENTER)
    public JSmileResult updateCart(@RequestBody @Valid ReqUpdateCart reqUpdateCart, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.TRADE_CENTER);
        reqUpdateCart.setCurUserId(jsmileContext.getUserId());
        reqUpdateCart.setCurTenantId(jsmileContext.getTenantId());
        return JSmileResult.success(cartFactory.updateCart(reqUpdateCart));
    }

    @RequestMapping(value = "/feign/cart/deleteCart", method = RequestMethod.POST)
    @JsmileLog(title = "订单中心->删除购物车", type = LogTypeEnum.DELETE, systemId = JSmileConstant.SystemId.TRADE_CENTER)
    public JSmileResult deleteCart(@RequestBody @Valid ReqDeleteCart reqDeleteCart, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.TRADE_CENTER);
        reqDeleteCart.setCurUserId(jsmileContext.getUserId());
        reqDeleteCart.setCurTenantId(jsmileContext.getTenantId());
        return JSmileResult.success(cartFactory.deleteCart(reqDeleteCart));
    }

    @RequestMapping(value = "/feign/cart/clearCart", method = RequestMethod.POST)
    @JsmileLog(title = "订单中心->清空购物车", type = LogTypeEnum.DELETE, systemId = JSmileConstant.SystemId.TRADE_CENTER)
    public JSmileResult clearCart(@RequestBody @Valid ReqCartDetial reqCartDetial, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.TRADE_CENTER);
        reqCartDetial.setCurUserId(jsmileContext.getUserId());
        reqCartDetial.setCurTenantId(jsmileContext.getTenantId());
        return JSmileResult.success(cartFactory.clearCart(reqCartDetial));
    }

    @RequestMapping(value = "/feign/cart/countCart", method = RequestMethod.POST)
    @JsmileLog(title = "订单中心->购物车数量", type = LogTypeEnum.QUERY, systemId = JSmileConstant.SystemId.TRADE_CENTER)
    public JSmileResult countCart(@RequestBody @Valid ReqCartDetial reqCartDetial, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.TRADE_CENTER);
        reqCartDetial.setCurUserId(jsmileContext.getUserId());
        reqCartDetial.setCurTenantId(jsmileContext.getTenantId());
        return JSmileResult.success(cartFactory.countCart(reqCartDetial));
    }

}
